package com.bwm.bwm007.main;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.widget.RadioGroup;

import com.bwm.bwm007.R;
import com.bwm.bwm007.main.fragment.FragmentA;
import com.bwm.bwm007.main.fragment.FragmentB;
import com.bwm.bwm007.main.fragment.FragmentC;
import com.bwm.bwm007.main.fragment.FragmentD;
import com.bwm.bwm007.main.util.CompressPic;
import com.bwm.bwm007.main.util.DisplayUtil;
import com.bwm.bwm007.main.util.IBtnCallListener;
import com.bwm.bwm007.main.util.LogUtil;


public class MainActivity extends FragmentActivity implements RadioGroup.OnCheckedChangeListener, IBtnCallListener {

    //RadioGroup
    private RadioGroup mRadioGroup;
    //Fragments
    private FragmentA mFragmentA;
    private FragmentB mFragmentB;
    private FragmentC mFragmentC;
    private FragmentD mFragemntD;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化控件
        initViews();
        LogUtil.getInstance().LogEor("程序内存大小:", CompressPic.getInstance().getAppMemory() + "M");
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.pic, null);
        LogUtil.getInstance().LogEor("PIC大小111:", "宽:" + DisplayUtil.px2dip(getApplication(), bitmap.getWidth()) + "...高:" + DisplayUtil.px2dip(getApplication(), bitmap.getHeight()));
        LogUtil.getInstance().LogEor("PIC大小:", (CompressPic.getInstance().decodeSampledBitmapFromResource(getResources(), R.mipmap.pic, 10, 10).getByteCount() / 1024) + "KB");
    }

    private void initViews() {
        mRadioGroup = (RadioGroup) findViewById(R.id.radiogroup);
        mRadioGroup.setOnCheckedChangeListener(this);
        //初始化默认界面
        if (mFragmentA == null) {
            mFragmentA = new FragmentA();
            addFragment(mFragmentA);
            showFragemtn(mFragmentA);
        } else {
            showFragemtn(mFragmentA);
        }
        //默认选中第一个
        mRadioGroup.check(R.id.btn1);
    }

    //添加Fragment
    private void addFragment(Fragment fragment) {
        FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
        ft.add(R.id.fragment_content, fragment);
        ft.commit();
    }

    //删除Fragment
    private void removeFragment(Fragment fragment) {
        FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
        ft.remove(fragment);
        ft.commit();
    }

    //显示Fragment
    private void showFragemtn(Fragment fragment) {
        FragmentTransaction ft = this.getSupportFragmentManager()
                .beginTransaction();
        // 设置Fragment的切换动画

        // 判断页面是否已经创建，如果已经创建，那么就隐藏掉
        if (mFragmentA != null) {
            ft.hide(mFragmentA);
        }
        if (mFragmentB != null) {
            ft.hide(mFragmentB);
        }
        if (mFragmentC != null) {
            ft.hide(mFragmentC);
        }
        if (mFragemntD != null) {
            ft.hide(mFragemntD);
        }
        ft.show(fragment);
        ft.commit();
    }


    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {

        switch (checkedId) {
            case R.id.btn1:
                //第一个界面
                if (mFragmentA == null) {
                    mFragmentA = new FragmentA();
                    addFragment(mFragmentA);
                    showFragemtn(mFragmentA);
                } else {
                    if (mFragmentA.isHidden()) {
                        showFragemtn(mFragmentA);
                    }
                }
                break;
            case R.id.btn2:
                //第二个界面
                if (mFragmentB == null) {
                    mFragmentB = new FragmentB();
                    if (!mFragmentB.isHidden()) {
                        addFragment(mFragmentB);
                        showFragemtn(mFragmentB);
                    }
                } else {
                    if (mFragmentB.isHidden()) {
                        showFragemtn(mFragmentB);
                    }
                }
                break;
            case R.id.btn3:
                //第三个界面
                if (mFragmentC == null) {
                    mFragmentC = new FragmentC();
                    if (!mFragmentC.isHidden()) {
                        addFragment(mFragmentC);
                        showFragemtn(mFragmentC);
                    }
                } else {
                    if (mFragmentC.isHidden()) {
                        showFragemtn(mFragmentC);
                    }
                }
                break;
            case R.id.btn4:
                //第四个界面
                if (mFragemntD == null) {
                    mFragemntD = new FragmentD();
                    if (!mFragemntD.isHidden()) {
                        addFragment(mFragemntD);
                        showFragemtn(mFragemntD);
                    }
                } else {
                    if (mFragemntD.isHidden()) {
                        showFragemtn(mFragemntD);
                    }
                }
                break;
            default:
                break;
        }
    }

    //定义接口
    private IBtnCallListener ibtncalllistener;

    @Override
    public void onAttachFragment(Fragment fragment) {
        try {
            ibtncalllistener = (IBtnCallListener) fragment;
        } catch (Exception e) {
            e.printStackTrace();
        }
        super.onAttachFragment(fragment);
    }

    @Override
    public void transferMsg(String string) {
        Log.e("MainActivity", "transferMsg:" + string);
    }
}